import { BugItem, BugQueryParams, BugResponse } from '@/g-types/bug';

// 模拟数据
const mockBugs: BugItem[] = [
  {
    id: '1',
    title: '登录页面加载缓慢',
    description: '用户反馈登录页面加载时间超过3秒，影响用户体验',
    status: 'pending',
    priority: 'high',
    reporter: '张三',
    createTime: '2024-03-27 10:00:00',
    updateTime: '2024-03-27 10:00:00',
    attachments: ['error-log.txt'],
  },
  {
    id: '2',
    title: '数据导出功能异常',
    description: '导出Excel时出现乱码，且部分数据丢失',
    status: 'processing',
    priority: 'medium',
    reporter: '李四',
    createTime: '2024-03-26 15:30:00',
    updateTime: '2024-03-27 09:15:00',
    attachments: ['export-error.xlsx'],
  },
  {
    id: '3',
    title: '移动端适配问题',
    description: '在iPhone 12上页面布局出现错位',
    status: 'resolved',
    priority: 'low',
    reporter: '王五',
    createTime: '2024-03-25 14:20:00',
    updateTime: '2024-03-26 16:45:00',
    attachments: ['mobile-screenshot.png'],
  },
  {
    id: '4',
    title: '表单验证不完整',
    description: '用户注册表单缺少手机号格式验证',
    status: 'pending',
    priority: 'medium',
    reporter: '赵六',
    createTime: '2024-03-24 11:10:00',
    updateTime: '2024-03-24 11:10:00',
  },
  {
    id: '5',
    title: '系统性能优化',
    description: '大数据量下列表页面加载缓慢',
    status: 'processing',
    priority: 'high',
    reporter: '钱七',
    createTime: '2024-03-23 16:40:00',
    updateTime: '2024-03-27 08:30:00',
    attachments: ['performance-report.pdf'],
  },
];

class BugServer {
  // 获取bug列表
  async getBugList(params: BugQueryParams): Promise<BugResponse> {
    // 模拟接口延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 模拟分页
    const { current, pageSize, ...filters } = params;
    let filteredData = [...mockBugs];

    // 模拟筛选
    if (filters.title) {
      filteredData = filteredData.filter(item => 
        item.title.toLowerCase().includes(filters.title!.toLowerCase())
      );
    }
    if (filters.status) {
      filteredData = filteredData.filter(item => item.status === filters.status);
    }
    if (filters.priority) {
      filteredData = filteredData.filter(item => item.priority === filters.priority);
    }
    if (filters.reporter) {
      filteredData = filteredData.filter(item => 
        item.reporter.toLowerCase().includes(filters.reporter!.toLowerCase())
      );
    }
    if (filters.createTime) {
      const [start, end] = filters.createTime;
      filteredData = filteredData.filter(item => {
        const itemTime = new Date(item.createTime).getTime();
        return itemTime >= new Date(start).getTime() && itemTime <= new Date(end).getTime();
      });
    }

    // 模拟分页
    const start = (current - 1) * pageSize;
    const end = start + pageSize;
    const list = filteredData.slice(start, end);

    return {
      total: filteredData.length,
      list,
    };
  }

  // 创建bug
  async createBug(data: Omit<BugItem, 'id' | 'createTime' | 'updateTime'>): Promise<void> {
    // 模拟接口延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 模拟创建
    const newBug: BugItem = {
      ...data,
      id: String(mockBugs.length + 1),
      createTime: new Date().toLocaleString(),
      updateTime: new Date().toLocaleString(),
    };
    mockBugs.push(newBug);
  }

  // 更新bug
  async updateBug(id: string, data: Partial<BugItem>): Promise<void> {
    // 模拟接口延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 模拟更新
    const index = mockBugs.findIndex(item => item.id === id);
    if (index !== -1) {
      mockBugs[index] = {
        ...mockBugs[index],
        ...data,
        updateTime: new Date().toLocaleString(),
      };
    }
  }

  // 删除bug
  async deleteBug(id: string): Promise<void> {
    // 模拟接口延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 模拟删除
    const index = mockBugs.findIndex(item => item.id === id);
    if (index !== -1) {
      mockBugs.splice(index, 1);
    }
  }
}

export const bugServer = new BugServer(); 